package com.apes.pi.parser;

import com.apes.framework.api.ApesParser;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.filereader.xml.ApesElement;
import com.apes.framework.util.ApesConst;
import com.apes.pi.model.PiQueue;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能：pi元素实现类
 *
 * @author xul
 * @create 2019-11-09 11:19
 */
public class PiQueueElement implements ApesElement {

    private PiQueue queue;
    private final Map<String, String> attributes = new HashMap<>();
    private ApesParser parser;

    public PiQueueElement(PiQueue queue) {
        this.queue = queue;
        setParser(SpringManager.getBean(QueueParser.class));
        setAttribute("method", queue.methodName());
        setAttribute("version", ApesConst.METHOD_VERSION);
        setAttribute("needInSession", "false");
        setAttribute("isTransaction", "true");
        setAttribute("description", queue.getName());
    }

    @Override
    public void setName(String name) {

    }

    @Override
    public String getName() {
        return queue.getName();
    }

    @Override
    public void setText(String text) {

    }

    @Override
    public String getText() {
        return null;
    }

    @Override
    public void setParent(ApesElement parent) {

    }

    @Override
    public ApesElement getParent() {
        return null;
    }

    @Override
    public void setAttribute(String key, String value) {
        Assert.notNull(key, "key must not be null");
        this.attributes.put(key, value);
    }

    @Override
    public String getAttribute(String key) {
        return this.attributes.get(key);
    }

    @Override
    public Map<String, String> getAttributes() {
        return this.attributes;
    }

    @Override
    public void setNestedElement(ApesElement element) {

    }

    @Override
    public Collection<ApesElement> getNestedElements() {
        return null;
    }

    @Override
    public void setParser(ApesParser parser) {
        this.parser = parser;
    }

    @Override
    public <T> T parse() {
        return (T) parser.parse(queue);
    }
}
